package com.qcxy.gzy.task;

import org.json.JSONObject;

import android.widget.Toast;

import com.qcxy.gzy.activity.R;
import com.qcxy.gzy.activity.ScanActivity;
import com.qcxy.gzy.app.ComApplication;
import com.qcxy.gzy.app.MNDFitness;

/**
 * 获取二维码图片Task
 * 
 * @author gzy
 *
 */
public class GetScanQRImageTask extends BaseAsyncTask<Void, Void, JSONObject> {
    private ScanActivity mActivity = null;

    public GetScanQRImageTask(ScanActivity activity) {
        super(activity);
        mActivity = activity;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    // 新开启的线程要执行的业务逻辑执行的方法
    @Override
    protected JSONObject doInBackground(Void... params) {
    	String mac = ComApplication.getLocalMacAddressFromWifiInfo();
        JSONObject jObject = MNDFitness.getQRCode(mac);
        return jObject;
    }

    // 要更新的UI写在此处
    @Override
    protected void onPostExecute(JSONObject result) {
        super.onPostExecute(result);
        if (result != null) {
            int resultCode = result.optInt("success");
            if (resultCode == MNDFitness.RESULT_CODE) {// 返回正常
                // 更新UI解析数据
                mActivity.getQRImage(result);
            } else if (resultCode == MNDFitness.RESULT_CODE_NOINFO) {
            	Toast.makeText(mActivity, R.string.scan_noDate, Toast.LENGTH_SHORT).show();
            } else {// 返回失败
                Toast.makeText(mActivity, R.string.scan_exception, Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(mActivity, R.string.scan_noNet, Toast.LENGTH_SHORT).show();
        }
    }
}
